<?php

namespace app\api\controller\v1;

use app\common\controller\Api;
use Sly\NotificationPusher\Adapter\Apns as ApnsAdapter;
use Sly\NotificationPusher\Collection\DeviceCollection;
use Sly\NotificationPusher\Model\Device;
use Sly\NotificationPusher\Model\Message;
use Sly\NotificationPusher\Model\Push;
use Sly\NotificationPusher\PushManager;
use think\Validate;

class Notification extends Api
{
    protected $noNeedLogin = 'index';
    protected $noNeedRight = 'index';

    public function index()
    {
        $validate = new Validate();
        if (!$validate->check(input(), [
            'token' => 'min:1'
        ])) {
            $this->error($validate->getError());
        }
        $token       = input('token', '015c5a2377ac146830f8ed09059b82bb91ed7190ebf74b633809bc2245da95ee');
        $pushManager = new PushManager(PushManager::ENVIRONMENT_DEV);
        $apnsAdapter = new ApnsAdapter([
            'certificate' => ROOT_PATH . 'certificate/apns.pem',
        ]);
        $push        = new Push($apnsAdapter, new DeviceCollection([
            new Device($token),
        ]), new Message('This is a basic example of push.'));
        $pushManager->add($push);
        $r = $pushManager->push();
        $this->success('', [
            'test' => $push->getResponses(),
            'r'    => $r,
        ]);
    }
}
